import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:handwrite/MainRoutePath.dart';
import 'package:handwrite/request/api/CommonApi.dart';
import 'package:handwrite/util/CommonUtil.dart';

import 'state.dart';

class InputLogic extends GetxController {
  final InputState state = InputState();

  Future<void> check() async {
    if (state.text.isEmpty) {
      showToast("请输入要生成的文字");
      return;
    }
    try {
      String res = (await CommonApi.check(state.text)).trim();
      if (res.isEmpty) {
        Get.defaultDialog(
            title: '提示',
            content: const Text("已入库，可以直接展示"),
            textConfirm: "查看",
            onConfirm: () {
              Get.back();
              Get.toNamed(MainRoutePath.show, arguments: state.text);
            },
            textCancel: "取消");
      } else {
        Get.defaultDialog(
            title: '提示',
            content: Text("还有 ‘$res’没有写入库"),
            textConfirm: "去录入",
            onConfirm: () {
              Get.back();
              Get.toNamed(MainRoutePath.write, arguments: res);
            },
            textCancel: "取消");
      }
    } catch (e) {
      print(e);
    }
  }

  void toShow() {
    Get.toNamed(MainRoutePath.show, arguments: state.text);
  }

  void rewrite() {
    Get.toNamed(MainRoutePath.write, arguments: state.text);
  }
}
